package com.dqhl.community.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.community.R;
import com.dqhl.community.adapter.OrderDetailAdapter;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.OrderDetail;
import com.dqhl.community.model.OrderParticulars;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LIENLIN on 2016/9/6.
 * 在线超市-配送详情页
 */
public class DistributionDetailActivity extends BaseActivity implements View.OnClickListener {

    private ImageView mIvback;
    private TextView mTvTitle;
    private ImageView mImOperation;
    private GridView mGv;//猜你喜欢
    private String order_id;
    private OrderDetail orderDetail;

    private ImageView iv_pic;
    private TextView tv_name;
    private TextView tv_number_money;
    private TextView tv_order_id;
    private TextView tv_status;
    private TextView tv_give_content;

    private ScrollView sv_content;
    private TextView tv_no_info;

    private ListView lv_goods;//订单listView
    private List<OrderParticulars> orderParticularses = new ArrayList<>();
    private OrderDetailAdapter orderDetailAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_distribution_detail);

        initView();

        getData();
    }

    private void getData() {
        showCustomLoadBar(R.string.bar_load_data);
        order_id = getIntent().getStringExtra("order_id");
        Dlog.e("orderId:" + order_id);
        RequestParams params = new RequestParams(Config.Api.supermarketOrderContent);
        params.addBodyParameter("order_id", order_id);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getInfo(result, "data");

                if (errCode == 0) {
                    orderDetail = JSON.parseObject(data, OrderDetail.class);
                    orderParticularses = orderDetail.getGoods();
                    Dlog.e("GGgetGoodds"+orderParticularses);
                    orderDetailAdapter = new OrderDetailAdapter(DistributionDetailActivity.this,orderParticularses);
                    lv_goods.setAdapter(orderDetailAdapter);
                    //显示订单状态
                    switch (orderDetail.getStatus()) {
                        case "1":
                            tv_status.setText("订单已生成");
                            break;
                        case "2":
                            tv_status.setText("已取消");
                            break;
                        case "3":
                            tv_status.setText("付款成功");
                            break;
                        case "4":
                            tv_status.setText("货到付款");
                            break;
                        case "5":
                            tv_status.setText("已发货");
                            break;
                        case "6":
                            tv_status.setText("配送中");
                            break;
                        case "7":
                            tv_status.setText("货到付款已支付");
                            break;
                        case "8":
                            tv_status.setText("买家签收");
                            break;
                        case "9":
                            tv_status.setText("他人代收");
                            break;
                        case "10":
                            tv_status.setText("签收异常");
                            break;
                        case "11":
                            tv_status.setText("退货");
                            break;
                        case "12":
                            tv_status.setText("退款");
                            break;
                        case "13":
                            tv_status.setText("订单完成");
                            break;
                    }
                    tv_give_content.setText(orderDetail.getGive_content().replaceFirst(" ", "\n"));
                } else {
                    sv_content.setVisibility(View.GONE);
                    tv_no_info.setVisibility(View.VISIBLE);
                    tv_no_info.setText(R.string.error_no_info);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                sv_content.setVisibility(View.GONE);
                tv_no_info.setVisibility(View.VISIBLE);
                tv_no_info.setText(R.string.error_net_connection);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    private void initView() {
        mIvback = (ImageView) findViewById(R.id.iv_top_back);
        mTvTitle = (TextView) findViewById(R.id.tv_top_title);
        mImOperation = (ImageView) findViewById(R.id.tv_top_operation);
//        mGv = (GridView) findViewById(R.id.gv_distribution);

//        iv_pic = (ImageView) findViewById(R.id.iv_pic);
//        tv_name = (TextView) findViewById(R.id.tv_name);
//        tv_number_money = (TextView) findViewById(R.id.tv_number_money);
//        tv_order_id = (TextView) findViewById(R.id.tv_order_id);
        tv_status = (TextView) findViewById(R.id.tv_status);
        tv_give_content = (TextView) findViewById(R.id.tv_give_content);

        sv_content = (ScrollView) findViewById(R.id.sv_content);
        tv_no_info = (TextView) findViewById(R.id.tv_no_info);

        lv_goods = (ListView) findViewById(R.id.lv_distribution);
        //头布局设置
        mIvback.setVisibility(View.VISIBLE);
        mImOperation.setVisibility(View.INVISIBLE);
        mIvback.setOnClickListener(this);
        mTvTitle.setText("在线超市");

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
        }
    }
}
